<?php
/**
 * 商品管理
 * 
 * ============================================================================
 * 版权所有: (c) 2009-2011，<UEShop Inc>，并保留所有权利。
 * 网站地址: http://www.ueshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用；
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 *
 * @author      cmpan
 * @copyright   Copyright (c) 2009-2010 UEShop Inc. (http://www.ueshop.cn)
 * @license     http://www.ueshop.cn/1_0.txt
 */
UE_Loader::loadModel('ProductModel', 'product');
class AdminController extends UE_Controller_Admin {
	/**
	 * 产品栏目
	 * @var array
	 */
	protected $_cats;
	
	/**
	 * 产品模型
	 *
	 * @var ProductModel
	 */
	protected $_product;
	
	public function init() {
		$this->_cats = CatModel::getCats('product');
		$this->_product = new ProductModel();
	}
	
	public function editAction() {
		if (empty($this->id)) {
			$this->showMessage('错误的请求');
			return false;
		}
		$this->_product->setId($this->id);
		
		if (IS_POST) {
			if($this->_product->edit($_POST)){
				$visitUrl = UE_Url::make('product/show/item/'.$this->id);
				$this->setOK('成功修改产品（<a href="'.$visitUrl.'" target="_blank">查看</a>）');
			} else {
				$this->setErr($this->_product->getErrs());
			}
		}
		
		$item = $this->_product->read();
		
		//print_r($r);
		include $this->tpl();
	}
	
	public function addAction() {
		if (IS_POST) {
			if($this->_product->add($_POST)){
				$visitUrl = UE_Url::make('product/show/item/'.$_POST['product_id']);
				$editUrl = UE_Url::make('product/admin/edit/'.$_POST['product_id']);
				$this->setOK('成功添加产品（<a href="'.$visitUrl.'" target="_blank">查看</a>, <a href="'.$editUrl.'" target="_blank">编辑</a>）');
			} else {
				$this->setErr($this->_product->getErrs());
			}
		}
		$ueid = makeUeid();
	    include $this->tpl();
	}
	
	public function listAction() {
		$list = array();
		$paging = $opts = '';
		
		if (IS_POST) {
			switch ($_POST['optype']){
				case 'sort':
					foreach ($_POST['product_order'] as $postId => $postOrder) {
						if(!$this->_product->setId($postId)->setOrder($postOrder)){
							$this->setErr($this->_product->getErrs());
						}
					}
					break;
				case 'pub':
					if (!empty($_POST['list-check'])) {
						foreach ($_POST['list-check'] as $postId => $postOrder) {
							if(!$this->_product->setId($postId)->setStatus(1)){
								$this->setErr($this->_product->getErrs());
							}
						}
					}
					break;
				case 'draft':
					if (!empty($_POST['list-check'])) {
						foreach ($_POST['list-check'] as $postId => $postOrder) {
							if(!$this->_product->setId($postId)->setStatus(0)){
								$this->setErr($this->_product->getErrs());
							}
						}
					}
					break;
				case 'trash':
					if (!empty($_POST['list-check'])) {
						foreach ($_POST['list-check'] as $postId => $postOrder) {
							if(!$this->_product->setId($postId)->setStatus(-1)){
								$this->setErr($this->_product->getErrs());
							}
						}
					}
					break;
				case 'del':if (!empty($_POST['list-check'])) {
						foreach ($_POST['list-check'] as $postId => $postOrder) {
							if(!$this->_product->setId($postId)->delete()){
								$this->setErr($this->_product->getErrs());
							}
						}
					}
					break;
			}
		}
		
		// 文章状态
		if (!isset($_GET['status']) || $_GET['status'] == 'all') {
			$opts .= ' AND product_status >= 0 ';
		} else {
			$opts .= " AND product_status = '{$_GET['status']}' ";
		}
		
		// 栏目，$_GET['cat_id']=0为未分类
		if (isset($_GET['cat_id']) && $_GET['cat_id'] != 'all') {
			$opts .= " AND cat_id IN({$this->_cats[$_GET['cat_id']]['chileId']}) ";			
		}
		
		// 搜索关键词
		if (!empty($_GET['keyword'])) {
			$opts .= " AND product_title LIKE '%{$_GET['keyword']}%' ";
		}
		
		// 显示排序
		$order = "  product_id DESC ";
		
		$totals = UE_DB::factory()->getfirstField("SELECT COUNT(*) FROM ue_product WHERE true $opts");
		
		if ($totals) {
			$pagingObj = new UE_Paging();
			$pagingObj->setVar($totals, 15);
			$paging = $pagingObj->pagingStr;
			
			$sql = "SELECT * FROM ue_product WHERE true $opts ORDER BY $order LIMIT {$pagingObj->firstCount}, {$pagingObj->pageNo}";
			$list = UE_DB::factory()->getAll($sql);			
		}
		
	    include $this->tpl();
	}
	
	/**
	 * 设置产品状态
	 *
	 * @return bool
	 */
	public function setStatusAction(){
		if (!$this->id || !isset($_GET['status']) || !in_array($_GET['status'], array(1, 0, -1))) {
			print json_encode(array('err'=>'错误的请求'));
			return false;
		}
		$this->_product->setId($this->id);
	    if($this->_product->setStatus($_GET['status'])) {
	    	print json_encode(array('err'=>''));
	    } else {
	    	print json_encode(array('err'=>$this->_product->getLastErr()));
	    }
	}
	
	/**
	 * 自定义网店设置
	 */
	public function customAction() {
	}
}